package com.lib.framework.utils

import android.text.format.Time
import com.lib.framework.manager.LogManager
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*


/**
 * Copyright (C), 2020-2020, 享物说
 * FileName: DateFormatUtil
 * Author: keanbin
 * Date: 2020-02-14 14:57
 * Description: 日期格式化工具类
 * History:
 * <author> <time> <version> <desc>
 * 作者姓名 修改时间 版本号 描述
 */
class DateUtil {

    companion object {
        /**
         * data 转为 String格式
         *
         * @param date
         * @param formatType 格式, 如yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒
         */
        fun dateToString(date: Date, formatType: String): String {
            return SimpleDateFormat(formatType, Locale.getDefault()).format(date)
        }

        /**
         * 毫秒时间 long格式 转 String格式
         */
        fun longToString(currentTime: Long, formatType: String): String {
            try {
                val format = SimpleDateFormat(formatType, Locale.getDefault())
                return format.format(currentTime)
            } catch (e: Exception) {
                e.printStackTrace()
            }

            return currentTime.toString()
        }

        /**
         * String格式 转 Date
         */
        fun stringToDate(strTime: String, formatType: String): Date {
            try {
                val formatter = SimpleDateFormat(formatType, Locale.getDefault())
                return formatter.parse(strTime)
            } catch (e: Exception) {
                LogManager.e("", e)
            }

            return Date()
        }

        /**
         * Long格式 转 Date
         */
        fun longToDate(currentTime: Long): Date {
            return Date(currentTime) // 根据long类型的毫秒数生命一个date类型的时间
        }

        /**
         * String格式 转 Long格式
         */
        fun stringToLong(strTime: String, formatType: String): Long {
            val date = stringToDate(strTime, formatType) // String类型转成date类型
            return dateToLong(date)
        }

        /**
         * Date 转 Long格式
         */
        fun dateToLong(date: Date): Long {
            return date.time
        }

        /**
         * 计算年龄
         */
        fun calcAge(birthTime: Long): Int {
           return calcAge(longToDate(birthTime))
        }

        /**
         * 计算年龄
         */
        fun calcAge(birthDay: Date?): Int {
            if (birthDay == null) return 0

            val cal = Calendar.getInstance()
            if (cal.before(birthDay)) {
                //出生日期晚于当前时间，无法计算
                return 0
            }

            val yearNow = cal[Calendar.YEAR] //当前年份
            val monthNow = cal[Calendar.MONTH] //当前月份
            val dayOfMonthNow = cal[Calendar.DAY_OF_MONTH] //当前日期

            cal.time = birthDay
            val yearBirth = cal[Calendar.YEAR]
            val monthBirth = cal[Calendar.MONTH]
            val dayOfMonthBirth = cal[Calendar.DAY_OF_MONTH]
            var age = yearNow - yearBirth //计算整岁数
            if (monthNow <= monthBirth) {
                if (monthNow == monthBirth) {
                    if (dayOfMonthNow < dayOfMonthBirth) age-- //当前日期在生日之前，年龄减一
                } else {
                    age-- //当前月份在生日之前，年龄减一
                }
            }
            return age
        }

        /**
         * 是否为当天24h内
         *
         * @param time 要判断是否在当天24h内的时间
         * @return boolean
         */
        fun isToday(time: Long): Boolean {
            val inputJudgeDate = Date(time)
            var flag = false

            //获取当前系统时间
            val longDate = System.currentTimeMillis()
            val nowDate = Date(longDate)
            val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
            val format = dateFormat.format(nowDate)
            val subDate = format.substring(0, 10)

            //定义每天的24h时间范围
            val beginTime = "$subDate 00:00:00"
            val endTime = "$subDate 23:59:59"
            try {
                val paseBeginTime = dateFormat.parse(beginTime)
                val paseEndTime = dateFormat.parse(endTime)

                if (paseBeginTime != null && paseEndTime != null &&
                    inputJudgeDate.after(paseBeginTime)
                    && inputJudgeDate.before(paseEndTime)) {
                    flag = true
                }
            } catch (e: ParseException) {
                e.printStackTrace()
            }
            return flag
        }

        /**
         * 判断当前系统时间是否在指定时间的范围内
         *
         * beginHour 开始小时,例如22
         * beginMin  开始小时的分钟数,例如30
         * endHour   结束小时,例如 8
         * endMin    结束小时的分钟数,例如0
         * true表示在范围内, 否则false
         */
        fun isCurrentInTimeScope(beginHour: Int, beginMin: Int, endHour: Int, endMin: Int): Boolean {
            var result = false
            val aDayInMillis = 1000 * 60 * 60 * 24.toLong()
            val currentTimeMillis = System.currentTimeMillis()
            val now = Time()
            now.set(currentTimeMillis)
            val startTime = Time()
            startTime.set(currentTimeMillis)
            startTime.hour = beginHour
            startTime.minute = beginMin
            val endTime = Time()
            endTime.set(currentTimeMillis)
            endTime.hour = endHour
            endTime.minute = endMin

            // 跨天的特殊情况(比如22:00-8:00)
            if (!startTime.before(endTime)) {
                startTime.set(startTime.toMillis(true) - aDayInMillis)
                result =
                    !now.before(startTime) && !now.after(endTime) // startTime <= now <= endTime
                val startTimeInThisDay = Time()
                startTimeInThisDay.set(startTime.toMillis(true) + aDayInMillis)
                if (!now.before(startTimeInThisDay)) {
                    result = true
                }
            } else { //普通情况(比如 8:00 - 14:00)
                result = !now.before(startTime) && !now.after(endTime) // startTime <= now <= endTime
            }
            return result
        }
    }
}